package service;

import mapper.BrandMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pagination.PageBean;
import pojo.Brand;
import utils.SqlSessionFactoryUtils;

import java.util.List;

public class BrandService {
    //    查询所有品牌（基本开发完毕）
    public List<Brand> selectAll() {
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        // 开启会话
        SqlSession sqlSession = factory.openSession();
        // 获取Mapper
        BrandMapper  brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3执行查询
        List<Brand> brands = brandMapper.selectAll();
        //4.关闭sqlSession
        sqlSession.close();
        //5.返回数据
        return brands;
    }
    //品牌新增
    public static void add(Brand brand){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        // 开启会话
        SqlSession sqlSession = factory.openSession();
        // 获取Mapper
        BrandMapper  brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3执行新增
        brandMapper.add(brand);
        sqlSession.commit();
        //5.关闭sqlSession
        sqlSession.close();

    }
//单个查询


    public Brand selectById(int id){
//调用BrandMapper.selectAll()
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
//2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
//3. 获取BrandMapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4. 调用方法
        Brand brand = brandMapper.selectById(id);
        sqlSession.close();
        return brand;
    }
    //品牌修改
    public static void update(Brand brand){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        // 开启会话
        SqlSession sqlSession = factory.openSession();
        // 获取Mapper
        BrandMapper  brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3执行修改
        brandMapper.update(brand);
        sqlSession.commit();
        //5.关闭sqlSession
        sqlSession.close();

    }
    //品牌状态查询
    public static List<Brand> states(int status,String brandName){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        // 开启会话
        SqlSession sqlSession = factory.openSession();
        // 获取Mapper
        BrandMapper  brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3执行查询
        List<Brand> Selectother = brandMapper.Selectother(status, brandName);

        sqlSession.commit();
        //5.关闭sqlSession
        sqlSession.close();
        return Selectother;
    }

    //    删除
    public void delete(int id){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
//3. 获取BrandMapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4. 调用方法
        brandMapper.deleteById(id);
//提交事务
        sqlSession.commit();
//释放资源
        sqlSession.close();

    }
    //分页查询
    public PageBean<Brand> selectAllPage(int currentPage, int pageSize) {
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        // 开启会话
        SqlSession sqlSession = factory.openSession();
        // 获取Mapper
        BrandMapper  brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3执行分页查询
        PageBean<Brand> pageBean = new PageBean<>();
//        3.1查询某一页数据
        int beginIndex = (currentPage - 1)*pageSize;
        List<Brand>brandList=brandMapper.selectAllPage(beginIndex,pageSize);
//        3.2查询总数据条数
        int totalCount = brandMapper.selectCount();
//        3.3封装分页数据
        pageBean.setData(brandList);
        pageBean.setTotalCount(totalCount);
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        int totalPage =(int) (Math.ceil(totalCount*1.0/pageSize));
        pageBean.setTotalPage(totalPage);
        //4.关闭sqlSession
        sqlSession.close();
        //5.返回数据
        return pageBean;
    }

    public void deleteByIds(int[] ids){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //4. 调用方法
        mapper.deleteByIds(ids);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

}



